package com.konsung.ksdc.manager.comment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.CommentInfoBean;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/5/19 0019.
 */
public class ProductCommentAdapter extends BaseAdapter {
    private List<CommentInfoBean> mDatas = new ArrayList<>();

    /**
     * 构造方法
     * @param commentInfoBeanList 数据
     */
    public ProductCommentAdapter(List<CommentInfoBean> commentInfoBeanList) {
        mDatas = commentInfoBeanList;
    }

    @Override
    public int getCount() {
        if (mDatas != null) {
            return mDatas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(MyApplication
                    .getContext())
                    .inflate(R.layout.item_listview_comment, null);
            new ViewHolder(convertView);
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();

        CommentInfoBean bean = mDatas.get(position);
        Picasso.with(MyApplication.getContext())
                .load(StringUtils.appendPicPath(bean.getUserPic()))
                .placeholder(R.drawable.pic_talkbox_sel)
                .error(R.drawable.pic_talkbox_sel)
                .into(holder.ivUserHead);
        holder.tvUserName.setText(bean.getEvaluater());
        holder.tvComment.setText(bean.getEvaluateTitle());
        holder.tvTime.setText(bean.getEvaluateTime());

        return convertView;
    }

    /**
     * 内部类
     */
    static class ViewHolder {
        @Bind(R.id.tv_user_name)
        TextView tvUserName;
        @Bind(R.id.tv_comment)
        TextView tvComment;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.iv_user_head)
        ImageView ivUserHead;

        /**
         * 类
         * @param view 控件
         */
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setTag(this);
        }
    }
}
